//
//  TopicDetailController.m
//  XhlTrip
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "TopicDetailController.h"
#import "LeaonWorking.h"
#import "TopDetailHeader.h"
#import "TopicModel.h"
#import "TopicdetailCell.h"
#import "MBProgressHUD.h"


@interface TopicDetailController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableDictionary *allDic;
@property (nonatomic, retain) NSMutableArray *articleArr;
@property (nonatomic, retain) TopDetailHeader *headerView;
@property (nonatomic, retain) NSMutableArray *secionArr;
@property (nonatomic, retain) NSMutableArray *sectitleArr;


@end

@implementation TopicDetailController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.allDic = [NSMutableDictionary dictionary];
        self.articleArr = [NSMutableArray array];
        self.secionArr = [NSMutableArray array];
        self.sectitleArr = [NSMutableArray array];
    }
    return self;
}


/* 网络请求 */
- (void)passVa:(NSString *)string
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:hud];
    hud.labelText = @"正在加载";
    [hud show:YES];

    NSString *str = [NSString stringWithFormat:@"%@%@%@", @"https://chanyouji.com/api/articles/", string, @".json"];
    NSString *nn = [NSString stringWithFormat:@"%@%@", @"topicdetail", string];
    [LeaonWorking netWorkingHanderGetGataWithUrl:str pathname:nn resultBlock:^(id result) {
        [self.allDic setDictionary:result];
        [self.articleArr addObjectsFromArray:[self.allDic objectForKey:@"article_sections"]];
        [self createHeader];
        
        /* 每个分区的数组 */
        [self.secionArr addObjectsFromArray: [self getSectionArray:self.articleArr]];
        /* 所有分区的标题 */
        [self.sectitleArr addObjectsFromArray:[self getAllTitleNoneIsNull:self.articleArr]];
        [_tableview reloadData];
        [hud removeFromSuperview];
        [hud release];
    }];
}


/* 有的有title 有的没有title 每个section的内容 */
- (NSMutableArray *)getSectionArray:(NSMutableArray *)paramArray {
    NSMutableArray *returnArray = [NSMutableArray array];
    NSMutableArray *array = nil;
    for (int i = 0; i < paramArray.count; i++) {
        NSDictionary *dic = [paramArray objectAtIndex:i];
        NSString *title = [dic objectForKey:@"title"];
        if (title.length > 0) {
            if (array != nil) {
                [returnArray addObject:array];
            }
            array = [NSMutableArray array];
            [array addObject:dic];
        } else {
            [array addObject:dic];
        }
        if (i == paramArray.count - 1) {
            [returnArray addObject:array];
        }
    }
    return returnArray;
}

/* title里全都有内容 */
- (NSMutableArray *)getAllTitleNoneIsNull:(NSMutableArray *)paramArr {
    NSMutableArray *arrayYouWant = [NSMutableArray array];
    // dic
    /* description , image_url , title ... 字典
     */
    for (NSDictionary *dic in paramArr) {
        NSString *title = [dic objectForKey:@"title"];
        if (title.length > 0) {
            [arrayYouWant addObject:dic];
        }
    }
    return arrayYouWant;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    [self createTableView];
    
}


- (void)createHeader
{
    TopicModel *topicModel = [[TopicModel alloc] init];
    [topicModel setValuesForKeysWithDictionary:self.allDic];
    self.headerView.model = topicModel;
}

- (void)createTableView
{
    self.tableview = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    _tableview.rowHeight = 200;
    
    self.headerView = [[[TopDetailHeader alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * Height)] autorelease];

    self.tableview.tableHeaderView = self.headerView;
    [self.view addSubview:self.tableview];
    [_tableview release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicdetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topicdetail"];
    if (cell == nil) {
        cell = [[TopicdetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"topicdetail"];
    }
    NSArray *arr = [self.secionArr objectAtIndex:indexPath.section];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    NSDictionary *dd = [arr objectAtIndex:indexPath.row];
    [cell.CellDic setDictionary:dd];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat textHeight;
    CGFloat photoHeight;
    NSArray *arr = [self.secionArr objectAtIndex:indexPath.section];
    NSDictionary *dd = [arr objectAtIndex:indexPath.row];
    if (![[dd objectForKey:@"image_url"] isEqualToString:@"" ]) {
        
        photoHeight = [TopicdetailCell heightWithImage:dd width:self.view.frame.size.width - 20 * Width];
    }else
    {
        photoHeight = 0;
    }
    
    if ([[dd objectForKey:@"description"] isKindOfClass:[NSNull class]] || [[dd objectForKey:@"description"] isEqualToString:@""]) {
        textHeight = 0;
    }else{
        
     textHeight = [TopicdetailCell heightWithText:[dd objectForKey:@"description"] width:self.view.frame.size.width - 20 * Width];
    }
       return textHeight + photoHeight + 40 * Height;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.sectitleArr[section] objectForKey:@"title"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [self.secionArr[section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectitleArr.count;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)dealloc
{
    [_sectitleArr release];
    [_secionArr release];
    [_headerView release];
    [_tableview release];
    [_articleArr release];
    [_allDic release];
    [super dealloc];
}
- (UIImage *)nameWithPath:(NSString *)paths
{
    NSString *path = [[NSBundle mainBundle] pathForResource:paths ofType:nil];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
